/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package enteties.article;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import util.article.GroupDetails;

/**
 *
 * @author Joris
 */
@Stateless
public class Group1Facade extends AbstractFacade<Group1> implements Group1FacadeLocal {
    @PersistenceContext(unitName = "Liebaut-ejbPU")
    private EntityManager em;

    @Override
    protected EntityManager getEntityManager() {
        return em;
        
    }

    public Group1Facade() {
        super(Group1.class);
    }
    
    @Override
    public Integer create (GroupDetails gd){
        Group1 group = new Group1();
        group.setName(gd.getName());
        group.setDescription(gd.getDescription());
        if(gd.getParentGroup() > -1){
            Group1 parent = find(gd.getParentGroup());
            group.setParentGroup(parent);
            parent.getGroup1Collection().add(group);
            edit(parent);
        }
        create(group);
        return group.getId();
    }
    
    @Override
    public void editGroup(GroupDetails gd){
        Group1 group = find(gd.getId());
        group.setName(gd.getName());
        group.setDescription(gd.getDescription());
        if(group.getParentGroup().getId() != gd.getParentGroup()){
            Group1 newParent = find(gd.getParentGroup());
            Group1 oldParent = group.getParentGroup();
            group.setParentGroup(newParent);
            oldParent.getGroup1Collection().remove(group);
            newParent.getGroup1Collection().add(group);
            edit(newParent);
            edit(oldParent);
        }
        
        edit(group);
    }
    
    @Override
    public void deleteGroup(Integer id){
        Group1 group = find(id);
        if(group.getGroup1Collection() != null){
            for(Group1 child:group.getGroup1Collection())
                deleteGroup(child);
            group.getGroup1Collection().clear();
        }
        group.getParentGroup().getGroup1Collection().remove(group);
        remove(group);
    }
    
    private void deleteGroup(Group1 group){
        if(group.getGroup1Collection() != null){
            for(Group1 child:group.getGroup1Collection())
                deleteGroup(group);
            group.getGroup1Collection().clear();
        }
        remove(group);
    }
    
    @Override
    public void addGroupToParent(Integer parentId, Integer groupId){
        Group1 parent = find(parentId);
        Group1 group = find(groupId);
        
        parent.getGroup1Collection().add(group);
        edit(parent);
    }
    
    @Override
    public void removeGroupFromParent(Integer parentId, Integer groupId){
        if(parentId > -1){
            Group1 parent = find(parentId);
        
            Collection<Group1> groups = parent.getGroup1Collection();
            for(Group1 child:groups){
                if(child.getId() == groupId){
                    groups.remove(child);
                    continue;
                }
            }
            edit(parent);
        }
    }
    
    @Override
    public Group1 findGroupByName(String name){
        List<Group1> groups = (List<Group1>) em.createNamedQuery("Group1.findByName").setParameter("name", name).getResultList();
        if (groups.size() == 1) {
            return groups.get(0);
        } else if (groups.size() > 1) {
            throw new IllegalStateException();
        } else {
            return null;
        }
    }
    
    @Override
    public Collection<GroupDetails> getAllRootGroups(){
        List<Group1> groups = (List<Group1>) em.createNamedQuery("Group1.findAllRoot").getResultList();
        if(groups.size() > 0)
            return convert(groups, true);
        else
            return null;
    }
    
    @Override
    public GroupDetails convert (Group1 g, boolean subGroups){
        if(g == null)
            return null;
        Integer parent = -1;
        if(g.getParentGroup() != null)
            parent = g.getParentGroup().getId();
        GroupDetails gd = new GroupDetails(g.getId(), g.getName(), g.getDescription(), parent);
        if(subGroups){
            for(Group1 child:g.getGroup1Collection())
                gd.addChildrenGroup(convert(child, true));
        }
        return gd;
    }
    
    @Override
    public Collection<GroupDetails> convert (Collection<Group1> groups, boolean subGroups){
        Collection<GroupDetails> details = new ArrayList<GroupDetails>();
        for(Group1 group : groups){
            details.add(convert(group, subGroups));
        }
        return details;
    }
    
}
